import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:flutter_localizations/flutter_localizations.dart';
// import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:get_storage/get_storage.dart';
import 'package:hockey/services/jzx_app.dart';
import 'package:hockey/services/jzx_config.dart';
// import 'package:hockey/services/jzx_rongcloud.dart';
import 'package:hockey/services/jzx_user.dart';
import 'package:lifecycle/lifecycle.dart';

import 'jzx_apps.dart';
import 'routes/jzx_app_pages.dart';

void main() async {
  await _initAsync();
  runApp(const JzxApp());
  await _initAsyncComplete();
}

_initAsync() async {
  // 启动图
  WidgetsFlutterBinding.ensureInitialized();
  // FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  await ScreenUtil.ensureScreenSize();

  // 初始化
  await _initStorage();

  if (Platform.isAndroid) {
    SystemUiOverlayStyle style = const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,

        ///这是设置状态栏的图标和字体的颜色
        ///Brightness.light  一般都是显示为白色
        ///Brightness.dark 一般都是显示为黑色
        statusBarIconBrightness: Brightness.dark);
    SystemChrome.setSystemUIOverlayStyle(style);
  }
}

// 启动后配置
Future _initAsyncComplete() async {
  // 调起主屏后，移除闪屏页
  // FlutterNativeSplash.remove();

  // 日志
  SystemChrome.setEnabledSystemUIMode(
    SystemUiMode.manual,
    // overlays: [],
    overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top],
  );
}

Future _initStorage() async {
  await GetStorage.init();
}

class JzxApp extends StatefulWidget {
  const JzxApp({super.key});

  @override
  State<JzxApp> createState() => _JzxAppState();
}

class _JzxAppState extends State<JzxApp> with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    // return ScreenUtilInit(
    //     designSize: const Size(375, 812),
    //     minTextAdapt: true,
    //     splitScreenMode: true,
    //     builder: (context, child) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      // 多主题切换
      // theme: ThemeManager.to.getThemeData(),
      // 仅有深色模式切换，可以用下列方式
      themeMode: ThemeManager.to.mode,
      theme: ThemeManager.to.themeData(),
      // darkTheme: ThemeManager.to.themeData(true),
      initialRoute: JzxAppPages.INITIAL,
      getPages: JzxAppPages.routes,
      // unknownRoute: AppPages.unknownRoute,
      // scrollBehavior: CustomScrollBehavior(),
      defaultTransition: GetPlatform.isMobile
          ? Transition.rightToLeftWithFade
          : Transition.fadeIn,
      routingCallback: (value) {
        // 切换页面清空当前 Toast
        JzxToast.hideLoading();
        context.keyboardDissmiss();
      },
      navigatorObservers: [
        defaultLifecycleObserver,
        JzxToast.toastNavigatorObserver(),
      ],
      binds: [
        // 挂载公共服务
        // Bind.put(JzxRCloudService()),
        Bind.put(JzxAppService()),
        Bind.put(JzxConfigService()),
        Bind.put(JzxUserService()),
      ],
      locale: const Locale('en', 'US'),
      fallbackLocale: const Locale('en', 'US'),
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        // Locale('zh', 'CN'),
        Locale('en', 'US'),
      ],
      builder: (ctx, child) {
        ScreenUtil.init(ctx,
            designSize: const Size(375, 812),
            minTextAdapt: true,
            splitScreenMode: true);
        return JzxToast.init(
          ctx,
          GestureDetector(
            onTap: () => ctx.keyboardDissmiss(),
            child: child!,
          ),
        );
      },
    );
    // });
  }
}
